﻿using System.Text;
using ICSharpCode.Decompiler.TypeSystem;

class Context
{
    private Dictionary<string, string> _names = new Dictionary<string, string>();
    private Dictionary<string, int> _nameCount = new Dictionary<string, int>();
    public IMethod CurrentMethod { get; set; }
    
    public string GetUniqueName(IMethod method)
    {
        var sb = new StringBuilder();
        sb.Append(method.FullName);
        for (int i = 0; i<method.Parameters.Count; i++)
        {
            var parameter = method.Parameters[i];
            if (i == 0)
                sb.Append('(');
            if (i > 0)
                sb.Append(',');
            sb.Append(parameter.Type.FullName);
            sb.Append(' ');
            sb.Append(parameter.Name);
            if (i == method.Parameters.Count - 1)
                sb.Append(')');
        }
        return sb.ToString();
    }
    public string GetMethodName(IMethod method)
    {
        var uniqueName = GetUniqueName(method);
        if (_names.TryGetValue(uniqueName, out var name))
            return name;
        name = method.Name;
        if (_nameCount.TryGetValue(name, out var index))
        {
            name += $"{name}_{++index}";
            _nameCount[name] = index;
        }
        else
            _nameCount[name] = 0;
        _names[uniqueName] = name;
        return method.Name;
    }
}

